﻿using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace Expression.Blend.SampleData.SampleDataSource
{
    using System;

    #region TwilioDataCollection

    public class TwilioDataCollection : TwilioDataItem, IGroupInfo
    {

        private List<TwilioDataItem> _itemCollection = new List<TwilioDataItem>();

        public Object Key
        {
            get { return this; }
        }

        public void Add(TwilioDataItem item)
        {
            _itemCollection.Add(item);
        }

        public IEnumerator<Object> GetEnumerator()
        {
            return _itemCollection.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    #endregion

    #region TwilioDataItem

    public class TwilioDataItem : INotifyPropertyChanged
    {

        private TwilioDataCollection _collection;
        private string _title = string.Empty;
        private string _subtitle = string.Empty;
        private ImageSource _image = null;
        private Uri _imageBaseUri = null;
        private String _imagePath = null;
        private string _content = string.Empty;


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public TwilioDataCollection Collection
        {
            get { return this._collection; }
            set
            {
                if (this._collection != value)
                {
                    this._collection = value;
                    this.OnPropertyChanged("Collection");
                }
            }
        }

        public string Title
        {
            get  { return this._title; }
            set
            {
                if (this._title != value)
                {
                    this._title = value;
                    this.OnPropertyChanged("Title");
                }
            }
        }

        public string Subtitle
        {
            get { return this._subtitle; }
            set
            {
                if (this._subtitle != value)
                {
                    this._subtitle = value;
                    this.OnPropertyChanged("Subtitle");
                }
            }
        }

        public ImageSource Image
        {
            get
            {
                if (_image == null && _imageBaseUri != null && _imagePath != null)
                {
                    _image = new BitmapImage(new Uri(_imageBaseUri, _imagePath));
                }
                return this._image;
            }

            set
            {
                if (this._image != value)
                {
                    this._image = value;
                    this._imageBaseUri = null;
                    this._imagePath = null;
                    this.OnPropertyChanged("Image");
                }
            }
        }

        public void SetImage(Uri baseUri, String path)
        {
            _image = null;
            _imageBaseUri = baseUri;
            _imagePath = path;
            this.OnPropertyChanged("Image");
        }

        public string Content
        {
            get { return this._content; }
            set
            {
                if (this._content != value)
                {
                    this._content = value;
                    this.OnPropertyChanged("Content");
                }
            }
        }
    }

    #endregion

    public class TwilioDataSource
    {
        public List<TwilioDataCollection> GroupedCollections { get; set; }

        public void AddCollection(String title, String subtitle, Uri baseUri, String imagePath, String content)
        {
            var collection = new TwilioDataCollection();
            collection.Title = title;
            collection.Subtitle = subtitle;
            collection.SetImage(baseUri, imagePath);
            collection.Content = content;
            GroupedCollections.Add(collection);
        }

        public void AddItem(String title, String subtitle, Uri baseUri, String imagePath, String content)
        {
            TwilioDataCollection lastCollection = GroupedCollections.LastOrDefault() as TwilioDataCollection;

            var item = new TwilioDataItem();
            item.Title = title;
            item.Subtitle = subtitle;
            item.SetImage(baseUri, imagePath);
            item.Content = content;
            item.Collection = lastCollection;
            if (lastCollection != null)
            {
                lastCollection.Add(item);
            }
        }

        public TwilioDataSource(Uri baseUri)
        {
            GroupedCollections = new List<TwilioDataCollection>();
        }
    }
}
